#include "OptionsState.h"

#include "Game.h"
#include "MainMenuState.h"
#include "tinyxml.h"
#include "SGD Wrappers/CSGD_XAudio2.h"


OptionsState::OptionsState() 
{
	optionsScreenID = -1;
	altOptionsID = -1;
	menuStuffID = -1;
	box1 = -1;
	box2 = -1;

	musicVolume = Game::GetInstance()->GetMusicVolume();
	sfxVolume   = Game::GetInstance()->GetSFXVolume();
}

OptionsState* OptionsState::GetInstance() 
{
	static OptionsState instance;
	return &instance;
}

OptionsState::~OptionsState()
{

}

void OptionsState::Enter() 
{
	// Getting bitmap font
	GroupFont =	CBitmapFont::GetInstance();

	if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(Game::GetInstance()->GetMenuMusic()) == false && fromMain == true)
		CSGD_XAudio2::GetInstance()->MusicPlaySong(Game::GetInstance()->GetMenuMusic(),true);

	// Load bitmap font
	FontImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Robovox.bmp"), D3DCOLOR_XRGB(0,0,0));
	optionsScreenID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/options_menu.png"));
	altOptionsID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/empty_menu.png"));
	menuStuffID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/options_menu_stuffUPDATE.png"));
	box1 = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/options_menuBox1.png"));
	box2 = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/options_menuBox2.png"));

	// Set the bitmap font's values
	GroupFont->SetValues(32, 32, 16, 14, ' ', FontImageID);

	// Cursor position
	cursorY = SFX;
	cursorX = 107;

	// Timer
	seanTimer = 0.0f;

	musicSliderX = 660;
	SFXSliderX   = 660;

	exitState = false;

	musicVolume = (int)(CSGD_XAudio2::GetInstance()->MusicGetMasterVolume()*100);
	sfxVolume   = (int)(CSGD_XAudio2::GetInstance()->SFXGetMasterVolume()*100);
}

void OptionsState::Exit()
{
	if(fromMain == false)
		CSGD_XAudio2::GetInstance()->MusicStopSong(Game::GetInstance()->GetMenuMusic());


	// Unload bitmap font
	if(FontImageID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(FontImageID);
		FontImageID = -1;
	}
	if(optionsScreenID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(optionsScreenID);
		optionsScreenID = -1;
	}	
	if(altOptionsID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(altOptionsID);
		altOptionsID = -1;
	}
	if(menuStuffID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(menuStuffID);
		menuStuffID = -1;
	}
	if(box1 != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(box1);
		box1 = -1;
	}
	if(box2 != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(box2);
		box2 = -1;
	}

	seanTimer = 0;

	SaveOptions("OptionsData.xml");

	fromMain = false;
}

bool OptionsState::Input() 
{
	Game* game = Game::GetInstance();

	// If escape return to main menu
	if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1))
		cursorY = BACK;

	// Move cursor up
	if(CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP) && cursorY == BACK)
	{
		cursorY = LANGAUGE;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}
	else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP) && cursorY != SFX)
	{
		cursorY -= OPTIONSincrement;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}
	else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP) && cursorY == SFX)
	{
		cursorY = BACK;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}

	// Move cursor down
	if(CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN) && cursorY == LANGAUGE)
	{
		cursorY = BACK;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}
	else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN) && cursorY != BACK)
	{
		cursorY += OPTIONSincrement;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}
	else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN) && cursorY == BACK)
	{
		cursorY = SFX;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}

	// Change Full Screen
	if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0) && cursorY == FULL /*&& cursorX != 740*/)
	{
		Game::GetInstance()->SetFullScreen(!Game::GetInstance()->GetFullscreen());

		CSGD_Direct3D::GetInstance()->ChangeDisplayParam(Game::GetInstance()->GetScreenWidth(), Game::GetInstance()->GetScreenHeight(), Game::GetInstance()->GetFullscreen());
		CSGD_DirectInput::GetInstance()->ClearInput();
		CSGD_DirectInput::GetInstance()->ReadDevices();

		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
	}
	if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0) && cursorY == LANGAUGE)
	{
		Game::GetInstance()->SetLanguage(!Game::GetInstance()->GetLanguage());
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
	}

	// Change music
	if(cursorY == MUSIC && CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT) && musicVolume < 100)
	{
		seanTimer += 0.01f;
		if(seanTimer >= 1)
		{
			musicVolume += 1;
			seanTimer = 0.0f;
		}
	}
	if(cursorY == MUSIC && CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT) && musicVolume >= 1)
	{
		seanTimer += 0.01f;
		if(seanTimer >= 1)
		{
			musicVolume -= 1;
			seanTimer = 0.0f;
		}
	}

	// Change SFX
	if(cursorY == SFX && CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT) && sfxVolume < 100)
	{
		seanTimer += 0.01f;
		if(seanTimer >= 1)
		{
			sfxVolume += 1;
			seanTimer = 0.0f;
		}
	}
	if(cursorY == SFX && CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT) && sfxVolume >= 1)
	{
		seanTimer += 0.01f;
		if(seanTimer >= 1)
		{
			sfxVolume -= 1;
			seanTimer = 0.0f;
		}
	}

	// Go back to the main menu
	if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6) || (CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0) && cursorY == BACK))
	{
		exitState = true;
		
		if(fromMain == true)
		{
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		}

		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuBack());

	}

	CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(musicVolume/100.0f);
	CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(sfxVolume/100.0f);

	return true;
}

void OptionsState::Update(float elapsed)
{
}

void OptionsState::Render() 
{
	if(Game::GetInstance()->GetLanguage() == true)
	{
		CSGD_TextureManager::GetInstance()->Draw(optionsScreenID,0,0);

		if(cursorY == SFX || cursorY == MUSIC || cursorY == BACK)
			CSGD_TextureManager::GetInstance()->Draw(box1,cursorX,cursorY);
		else if(cursorY == FULL || cursorY == LANGAUGE)
			CSGD_TextureManager::GetInstance()->Draw(box2,cursorX,cursorY);
	}
	else
	{
		CSGD_TextureManager::GetInstance()->Draw(altOptionsID,0,0);

		// Selected and not selected colors
		DWORD isSelected = D3DCOLOR_XRGB(255,0,0);
		DWORD notSelected =  D3DCOLOR_XRGB(0,255,255);

		// Title of options
		GroupFont->PrintTCHAR(optionStr, 75, 50, 3.0f, D3DCOLOR_ARGB(255,0,255,255));

		//{MUSIC = 150, SFX = 230, FULL = 310, LANGAUGE = 390, BACK = 470, OPTIONSincrement = 80};

		// Options menu
		GroupFont->PrintTCHAR(musicStr,		125, MUSIC,		cursorY==MUSIC ? 1.5f : 1.0f, cursorY==MUSIC ? isSelected : notSelected);
		GroupFont->PrintTCHAR(soundStr,		125, SFX,		cursorY==SFX ? 1.5f : 1.0f, cursorY==SFX   ? isSelected : notSelected);
		GroupFont->PrintTCHAR(fullStr,		125, FULL,		cursorY==FULL ? 1.5f : 1.0f, cursorY==FULL  ? isSelected : notSelected);
		GroupFont->PrintTCHAR(languageStr,	125, LANGAUGE,	cursorY==LANGAUGE ? 1.3f : 1.0f, cursorY==LANGAUGE ? isSelected : notSelected);
		GroupFont->PrintTCHAR(backStr,		125, BACK,		cursorY==BACK ? 1.5f : 1.0f,  cursorY==BACK ? isSelected : notSelected);

		RECT cursor = {18,18,66,74};
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,cursorX,cursorY,1.0f,1.0f,&cursor);
	}



	// Sound Bars
	RECT bar = {16,96,277,120};
	CSGD_TextureManager::GetInstance()->Draw(menuStuffID,400,155,1.0f,1.0f,&bar);
	CSGD_TextureManager::GetInstance()->Draw(menuStuffID,400,240,1.0f,1.0f,&bar);

	// Sound Sliders
	RECT slider = {18,130,34,172};
	CSGD_TextureManager::GetInstance()->Draw(menuStuffID,(int)(musicSliderX-270+(musicVolume*2.7f)),230,1.0f,1.0f,&slider);
	CSGD_TextureManager::GetInstance()->Draw(menuStuffID,(int)(SFXSliderX-270+(sfxVolume*2.7f)),145,1.0f,1.0f,&slider);


	// Full Screen //
	RECT greenBox = {158,24,200,67};

	if(Game::GetInstance()->GetFullscreen() == false)			// OFF
	{
		RECT fs = {350,100,394,144};
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,540,310,1.0f,1.0f,&fs);
	}
	else if(Game::GetInstance()->GetFullscreen() == true)		// ON
	{
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,540,310,1.0f,1.0f,&greenBox);
	}

	// Language //
	if(Game::GetInstance()->GetLanguage() == true)				// EN
	{
		RECT en = {225,24,269,68};
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,540,390,1.0f,1.0f,&en);

		RECT nl = {419,24,463,68};
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,590,390,1.0f,1.0f,&nl);
	}
	else if(Game::GetInstance()->GetLanguage() == false)		// NL
	{
		RECT nl = {350,24,394,68};
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,590,390,1.0f,1.0f,&nl);

		RECT en = {294,24,338,68};
		CSGD_TextureManager::GetInstance()->Draw(menuStuffID,540,390,1.0f,1.0f,&en);
	}


	// Position of music and sfx on the screen
	int musicPosition = 500+musicVolume*3;
	int SFXPositon = 500+sfxVolume*3;



	// Draw lines and box for music and sfx
	//RECT musicBox;
	//musicBox.left = musicPosition-10-100;
	//musicBox.right = musicPosition+10-100;
	//musicBox.top = 240;
	//musicBox.bottom = 260;
	//RECT SFXBox;
	//SFXBox.left = SFXPositon-10-100;
	//SFXBox.right = SFXPositon+10-100;
	//SFXBox.top = 340;
	//SFXBox.bottom = 360;
	//CSGD_Direct3D::GetInstance()->DrawLine(500-100,250,800-100,250,0,255,255);
	//CSGD_Direct3D::GetInstance()->DrawLine(500-100,350,800-100,350,0,255,255);
	//CSGD_Direct3D::GetInstance()->DrawRect(musicBox, cursorY==250 ? 255 : 0, cursorY==250 ? 0 : 255, cursorY==250 ? 0 : 255);
	//CSGD_Direct3D::GetInstance()->DrawRect(SFXBox, cursorY==350 ? 255 : 0, cursorY==350 ? 0 : 255, cursorY==350 ? 0 : 255);
}

void OptionsState::SaveOptions(const char* szFileName)
{
	// Create a TinyXML Document
	TiXmlDocument doc;

	// Create a TinyXML Declaration (version info)
	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0","utf-8","");
	// Add the declaration to the document
	doc.LinkEndChild(pDec);
	
	// Create a root node for the document (sound_list)
	TiXmlElement* pRoot = new TiXmlElement("options");
	// Add the root node to the document
	doc.LinkEndChild(pRoot);


	// Create a node for the sounds
	TiXmlElement* pSound = new TiXmlElement("sound_info");
	// Fill the node with sounds info
	pSound->SetAttribute("music",(int)(CSGD_XAudio2::GetInstance()->MusicGetMasterVolume()*100));
	pSound->SetAttribute("sfx",	(int)(CSGD_XAudio2::GetInstance()->SFXGetMasterVolume()*100));
	// Add the sounds node to the "options"
	pRoot->LinkEndChild(pSound);

	// Create a node for the screen
	TiXmlElement* pScreen = new TiXmlElement("screen");
	// Fill the node with screen info
	if(Game::GetInstance()->GetFullscreen() == true)
		pScreen->SetAttribute("fullscreen",0);
	else
		pScreen->SetAttribute("fullscreen",1);

	// Add the sounds node to the "options"
	pRoot->LinkEndChild(pScreen);

	// Create a node for the language
	TiXmlElement* pLanguage = new TiXmlElement("language");
	// Fill the node with language info
	if(Game::GetInstance()->GetLanguage() == true)
		pLanguage->SetAttribute("english",1);
	else
		pLanguage->SetAttribute("english",0);
	// Add the sounds node to the "options"
	pRoot->LinkEndChild(pLanguage);


	// Save the XML document to the file
	doc.SaveFile(szFileName);
}